﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace Event_SG_EWDT
{
    public partial class Events : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                EventClick(liAll, null); //fake a click

                Resetddl();
            }
            foreach (Control c in ulList.Controls)
            {
                if (c is System.Web.UI.HtmlControls.HtmlAnchor)
                {
                    ((System.Web.UI.HtmlControls.HtmlAnchor)c).ServerClick += new EventHandler(EventClick);
                }
            }
        }




        void Resetddl()
        {
            ddl_EventPageNumber.Items.Clear();
            int pagenumber = (int)Math.Ceiling(Convert.ToDouble(((ArrayList)Session["DisplayEvent"]).Count) / 6); // generate the Value in DDL
            for (int i = 0; i < pagenumber; i++)
            {
                ddl_EventPageNumber.Items.Add((i + 1).ToString());
            }
        }

        public void EventClick(object sender, EventArgs e)
        {
            
            switch (((System.Web.UI.HtmlControls.HtmlAnchor)sender).ID)
            {

                case "liAll":
                    divEventHightlights.Controls.Clear();
                    Session["DisplayEvent"] = DBQUERIES.GetTopEventFromEachType();
                    ShowInEventDiv((ArrayList) Session["DisplayEvent"], 0);
                    Resetddl();
                    break;
                case "liArt":
                    divEventHightlights.Controls.Clear();
                    Session["DisplayEvent"] = DBQUERIES.SearchEventViaCategory(2);
                    ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
                    Resetddl();
                    
                    break;
                case "liBusiness":
                    divEventHightlights.Controls.Clear();
                    Session["DisplayEvent"] = DBQUERIES.SearchEventViaCategory(3);
                    ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
                    Resetddl();

                    break;
                case "liCharity":
                                        divEventHightlights.Controls.Clear();
                    Session["DisplayEvent"] = DBQUERIES.SearchEventViaCategory(4);
                    ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
                    Resetddl();

                    break;
                case "liCourses":
                                        divEventHightlights.Controls.Clear();
                    Session["DisplayEvent"] = DBQUERIES.SearchEventViaCategory(5);
                    ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
                    Resetddl();

                    break;
                case "liDining":
                                        divEventHightlights.Controls.Clear();
                    Session["DisplayEvent"] = DBQUERIES.SearchEventViaCategory(6);
                    ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
                    Resetddl();

                    break;
                case "liExhibitions":
                                        divEventHightlights.Controls.Clear();
                    Session["DisplayEvent"] = DBQUERIES.SearchEventViaCategory(7);
                    ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
                    Resetddl();

                    break;
                case "liFamily":
                                        divEventHightlights.Controls.Clear();
                    Session["DisplayEvent"] = DBQUERIES.SearchEventViaCategory(8);
                    ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
                    Resetddl();
                    
                    break;
                case "liFashion":
                                        divEventHightlights.Controls.Clear();
                    Session["DisplayEvent"] = DBQUERIES.SearchEventViaCategory(9);
                    ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
                    Resetddl();
                    
                    break;
                case "liHousing":
                                        divEventHightlights.Controls.Clear();
                    Session["DisplayEvent"] = DBQUERIES.SearchEventViaCategory(10);
                    ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
                    Resetddl();
                    
                    break;
                case "liMotoring":
                                        divEventHightlights.Controls.Clear();
                    Session["DisplayEvent"] = DBQUERIES.SearchEventViaCategory(11);
                    ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
                    Resetddl();
                    
                    break;
                case "liMusic":
                                        divEventHightlights.Controls.Clear();
                    Session["DisplayEvent"] = DBQUERIES.SearchEventViaCategory(12);
                    ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
                    Resetddl();
                    
                    break;
                case "liNightlife":
                                        divEventHightlights.Controls.Clear();
                    Session["DisplayEvent"] = DBQUERIES.SearchEventViaCategory(13);
                    ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
                    Resetddl();
                    
                    break;
                case "liPets":
                                        divEventHightlights.Controls.Clear();
                    Session["DisplayEvent"] = DBQUERIES.SearchEventViaCategory(14);
                    ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
                    Resetddl();
                    break;
                case "liShopping":
                                        divEventHightlights.Controls.Clear();
                    Session["DisplayEvent"] = DBQUERIES.SearchEventViaCategory(15);
                    ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
                    Resetddl();
                    break;
                case "liSports":
                                        divEventHightlights.Controls.Clear();
                    Session["DisplayEvent"] = DBQUERIES.SearchEventViaCategory(16);
                    ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
                    Resetddl();
                    break;
                case "liTravel":
                                        divEventHightlights.Controls.Clear();
                    Session["DisplayEvent"] = DBQUERIES.SearchEventViaCategory(17);
                    ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
                    Resetddl();
                    break;


            }
        }
        
        Control CreateCustomControl(string eventid, string eventtitle, string desc)
        {
            Control c = LoadControl("~/CustomUserControl.ascx");

            HyperLink hyperlinkEvent = (HyperLink)c.FindControl("hyperlinkEvent");
            TextBox tbxUserControl = (TextBox)c.FindControl("tbxUserControl");
            Image imgUserControl = (Image)c.FindControl("imgUserControl");

            hyperlinkEvent.Text = eventtitle;
            hyperlinkEvent.NavigateUrl += String.Format("~/ViewEvent.aspx?eventid={0}", eventid);

            tbxUserControl.Text = desc;

            //************************************************ NEED TO UPDATE
            //imgUserControl.ImageUrl = "~/images/events/" + eventid + "/1.jpg";
            string id = DBMANAGER.GetImageIdEventThumbnail(eventid);
            imgUserControl.ImageUrl = "~/HTTPHandler.ashx?imageid=" + Convert.ToInt32(id);


            return c;
        }
        
        //void ShowInEventDiv(ArrayList a)
        //{
        //    foreach (object o in a)
        //    {
        //        string[] s = (string[])o;

        //        Control c = CreateCustomControl(s[0], s[1], s[2]);

        //        divEventHightlights.Controls.Add(c);
        //    }

        //}
        
        void ShowInEventDiv(ArrayList a, int x)
        {
            if (a.Count >= 6 + (6 * x)) // if the number of events are greater than the option.
            {
                for (int i = 6*x; i < 6 + (6 * x); i++)
                {
                    string[] s = (string[])a[i];

                    Control c = CreateCustomControl(s[0], s[1], s[2]);

                    divEventHightlights.Controls.Add(c);
                }
            }
            else
            {
                if (a.Count != 0)
                {
                    int left = (a.Count + 6) % (6 + (6 * x));
                    for (int i = x * 6; i < left + (6 * x); i++)
                    {
                        string[] s = (string[])a[i];

                        Control c = CreateCustomControl(s[0], s[1], s[2]);

                        divEventHightlights.Controls.Add(c);
                    }
                }

            }
        }
        
        protected void tbxSearch_TextChanged(object sender, EventArgs e)
        {
            //Session["DisplayEvent"] = DBQUERIES.SearchEvent(tbxSearchEvent.Text);
            //divEventHightlights.Controls.Clear();
            //ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
            //Resetddl();
        }

        protected void ddl_pageNumber_SelectedIndexChanged(object sender, EventArgs e)
        {
            int page = Convert.ToInt32(ddl_EventPageNumber.SelectedValue) - 1;
           divEventHightlights.Controls.Clear();
           ShowInEventDiv((ArrayList)Session["DisplayEvent"], page); ;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["DisplayEvent"] = DBQUERIES.SearchEvent(tbxSearchEvent.Text);
            divEventHightlights.Controls.Clear();
            ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
            Resetddl();
        }

        protected void btnSearchEventDummy_Click(object sender, EventArgs e)
        {
            Session["DisplayEvent"] = DBQUERIES.SearchEvent(tbxSearchEvent.Text);
            divEventHightlights.Controls.Clear();
            ShowInEventDiv((ArrayList)Session["DisplayEvent"], 0);
            Resetddl();
        }

        //        divEventHightlights.Controls.Clear();

        //        List<FakeClass> fakeList = new List<FakeClass>(); //Simulate data in SQL...

        //        switch (((System.Web.UI.HtmlControls.HtmlAnchor)sender).ID)
        //        {

        //            //do some sql to get respective events

        //            case "liAll":
        //                fakeList.Add(new FakeClass() { heading = Utility.GenerateNewPassword(), desc = Utility.GenerateNewPassword(), imageUrl = "~/images/cloud.png" });
        //                fakeList.Add(new FakeClass() { heading = Utility.GenerateNewPassword(), desc = Utility.GenerateNewPassword(), imageUrl = "~/images/cloud.png" });
        //                fakeList.Add(new FakeClass() { heading = Utility.GenerateNewPassword(), desc = Utility.GenerateNewPassword(), imageUrl = "~/images/cloud.png" });
        //                fakeList.Add(new FakeClass() { heading = Utility.GenerateNewPassword(), desc = Utility.GenerateNewPassword(), imageUrl = "~/images/cloud.png" });
        //                fakeList.Add(new FakeClass() { heading = Utility.GenerateNewPassword(), desc = Utility.GenerateNewPassword(), imageUrl = "~/images/cloud.png" });
        //                fakeList.Add(new FakeClass() { heading = Utility.GenerateNewPassword(), desc = Utility.GenerateNewPassword(), imageUrl = "~/images/cloud.png" });
        //                break;
        //            case "liBusiness":
        //                fakeList.Add(new FakeClass() { heading = Utility.GenerateNewPassword(), desc = Utility.GenerateNewPassword(), imageUrl = "~/images/facebook.png" });
        //                fakeList.Add(new FakeClass() { heading = Utility.GenerateNewPassword(), desc = Utility.GenerateNewPassword(), imageUrl = "~/images/facebook.png" });
        //                fakeList.Add(new FakeClass() { heading = Utility.GenerateNewPassword(), desc = Utility.GenerateNewPassword(), imageUrl = "~/images/facebook.png" });
        //                break;
        //            case "liTechology":
        //                fakeList.Add(new FakeClass() { heading = Utility.GenerateNewPassword(), desc = Utility.GenerateNewPassword(), imageUrl = "~/images/p.png" });
        //                break;
        //            case "liNightLife":
        //                fakeList.Add(new FakeClass() { heading = Utility.GenerateNewPassword(), desc = Utility.GenerateNewPassword(), imageUrl = "~/images/twitter.png" });
        //                fakeList.Add(new FakeClass() { heading = Utility.GenerateNewPassword(), desc = Utility.GenerateNewPassword(), imageUrl = "~/images/twitter.png" });
        //                break;
        //            case "liSports":
        //                fakeList.Add(new FakeClass() { heading = Utility.GenerateNewPassword(), desc = Utility.GenerateNewPassword(), imageUrl = "~/images/feed.png" });
        //                break;
        //            case "liMusical":
        //                fakeList.Add(new FakeClass() { heading = Utility.GenerateNewPassword(), desc = Utility.GenerateNewPassword(), imageUrl = "~/images/search.png" });
        //                break;
        //            case "liPets":
        //                break;

        //        }

        //        foreach (FakeClass f in fakeList)
        //        {
        //            Control c = LoadControl("~/CustomUserControl.ascx");

        //            HyperLink hyperlinkEvent = (HyperLink)c.FindControl("hyperlinkEvent");

        //            TextBox tbxUserControl = (TextBox)c.FindControl("tbxUserControl");

        //            Image imgUserControl = (Image)c.FindControl("imgUserControl");

        //            //hyperlinkEvent.Text = Utility.GenerateNewPassword();

        //            //tbxUserControl.Text = Utility.GenerateNewPassword();

        //            //imgUserControl.ImageUrl = "~/images/facebook.png";

        //            hyperlinkEvent.Text = f.heading;

        //            hyperlinkEvent.NavigateUrl += String.Format("~/ViewEvent.aspx?eventid={0}", 1234567);

        //            tbxUserControl.Text = f.desc;

        //            imgUserControl.ImageUrl = f.imageUrl;




        //            divEventHightlights.Controls.Add(c);
        //        }




        //    }
        //}

        //public class FakeClass
        //{
        //    public string heading { get; set; }
        //    public string desc { get; set; }
        //    public string imageUrl { get; set; }
        //}
    }
}